package com.protocols.sctp.m3ua;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.nio.ByteBuffer;
import java.util.Arrays;

import com.protocols.*;

public class RoutingContext implements Serializable{
	private OctaveInteger ParameterTag= new OctaveInteger(6,2); // Routing Context
	private OctaveInteger ParameterLength= new OctaveInteger(8,2);	// Length=8
	private OctaveInteger RoutingContext= new OctaveInteger(4);
	private Boolean Visible=false;
	private Boolean Mandatory =true;
	


	
	public RoutingContext(Boolean ismandatory) {
		Mandatory=ismandatory;
	}
	
	public RoutingContext(int routingcontext,Boolean mandatory) {		
		Mandatory=mandatory;		
		RoutingContext.Value=routingcontext;
		Visible=true;
		
	}

	
	public byte [] getByteArray(){		
		return  !Visible?new byte[0]:ProtoOperations.concatArrays(ParameterTag.getByteArray(), ParameterLength.getByteArray(), RoutingContext.getByteArray());
		

	}
	
	
	
	public int getLength(){

		return getByteArray().length;
		
	}


	public int getParameterTag() {
		return ParameterTag.Value;
	}



	public int getParameterLength() {
		return Visible?ParameterLength.Value:0;
	}




	public int getRoutingContext() {
		return RoutingContext.Value;
	}


	public void setRoutingContext(int routingcontext) {

		RoutingContext.Value = routingcontext;
		Visible=true;
	}

	public Boolean getVisible() {
		return Visible;
	}

	public void setVisible(Boolean visible) {
		Visible = visible;
	}
	
	public byte [] decode(byte [] input) throws ProtocolStackException{
		
		if (input.length==0)
			return new byte[0];
		else if (input.length<8 && input.length>0)
			throw new ProtocolStackException("Decode Failure:RoutingContext"+ProtoOperations.getHexString(input));
		
		
		if (input[0]==0 && input[1]==6 && input[2]==0 && input[3]==8)
		{			
			ParameterLength.Value=ProtoOperations.intFromByteArray(input, 2, 2);
			setRoutingContext( ProtoOperations.intFromByteArray(input, 4,  ParameterLength.Value-4));
			return ProtoOperations.extractByteArray(input, ParameterLength.Value, input.length-ParameterLength.Value);
		}
		else if (isMandatory())
			throw new ProtocolStackException("Decode Failure:RoutingContext"+ProtoOperations.getHexString(input));
		else
				return input;
					
		}

		public Boolean isMandatory() {
			return Mandatory;
		}

		public void setMandatory(Boolean mandatory) {
			Mandatory = mandatory;
		}

}
